/*
 * Copyright (c) 2007 T.Cogghe, D.Leroux, S.Lévignac, S.Rives
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
#ifndef __IMAGE_RECADREE_H__
#define __IMAGE_RECADREE_H__

#include "Image.hh"
#include "Matrice.hh"

class ImageRecadree : public Image {
private:
	Matrice xfer;
	unsigned char pixel[4];
public:
	ImageRecadree(Gtk::Image*i) : Image(i), xfer() {
		pixel[3]=pixel[2]=pixel[1]=pixel[0]=0;
	}
	ImageRecadree(Glib::RefPtr<Gtk::Image> i) : Image(i), xfer() {
		pixel[3]=pixel[2]=pixel[1]=pixel[0]=0;
	}
	ImageRecadree(Glib::RefPtr<Gdk::Pixbuf> p) : Image(p), xfer() {
		pixel[3]=pixel[2]=pixel[1]=pixel[0]=0;
	}
	ImageRecadree(std::string filename) : Image(filename), xfer() {
		pixel[3]=pixel[2]=pixel[1]=pixel[0]=0;
	}

	virtual ~ImageRecadree() {}
	const Matrice& matrice() const { return xfer; }
	Matrice& matrice() { return xfer; }
	unsigned char* operator() (int xi, int yi) const {
		unsigned char*pix=(unsigned char*)pixel;
		/* on va récupérer un carré de 4 pixels dans l'image, et faire une interpolation
			+-----+-----+
			|1    |    2|
			|  +--+--+  |
			+--+--+--+--+
			|  | x,y |  |
			|3 +--+--+ 4|
			+-----+-----+
		 */
		Vecteur v(xi,yi,1);
		Vecteur pos=xfer*v;
		int	x1 = (int)pos(0), y1=(int)pos(1),
			x2 = x1+1, y2 = y1,
			x3 = x1,   y3 = y1+1,
			x4 = x1+1, y4 = y1+1;
		unsigned char
			*p1 = this->Image::operator()(x1,y1),
			*p2 = this->Image::operator()(x2,y2),
			*p3 = this->Image::operator()(x3,y3),
			*p4 = this->Image::operator()(x4,y4);
		double	coef1 = (1-(pos(0)-x1))*(1-(pos(1)-y1)),
			coef2 = (1-(x2-pos(0)))*(1-(pos(1)-y2)),
			coef3 = (1-(pos(0)-x3))*(1-(y3-pos(1))),
			coef4 = (1-(x4-pos(0)))*(1-(y4-pos(1)));
//		cout<<"debug "<<(void*)p1<<' '<<(void*)p2<<' '<<(void*)p3<<' '<<(void*)p4
//			<<' '<<coef1<<' '<<coef2<<' '<<coef3<<' '<<coef4<<endl;
		for(int i=0;i<3;i++) {
			pix[i]=(unsigned char) (
				(p1[i]*coef1)
				 + (p2[i]*coef2)
				 + (p3[i]*coef3)
				 + (p4[i]*coef4)
			);
		}
		return pix;
	}
};

#endif
